<?php

class Connect
{
    public function __construct(string $host, int $port, string $db, string $user, string $password)
    {
        $this->sql_pdo = new PDO("mysql:host=$host;port=$port;dbname=$db", $user, $password, [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ]);
    }

    public function exec(string $sql, bool $select = true)
    {
        if ($select) {
            $st = $this->sql_pdo->query($sql, PDO::FETCH_ASSOC);
            if (false === $st) {
                return false;
            }
            return $st->fetchAll();
        }
        if (false === $this->sql_pdo->beginTransaction()) {
            return false;
        }
        $this->sql_pdo->exec($sql);
        try {
            $this->sql_pdo->commit();
        } catch (PDOException $e) {
            $this->sql_pdo->rollBack();
            return false;
        }
        return true;
    }
}
